package client.ui.view;

import client.assets.Icons;
import client.control.Controller;
import client.control.ResponseEntity;
import client.entity.Good;
import client.ui.util.MaterialColors;
import client.ui.view.base.FrameBase;
import client.ui.view.base.PanelControl;
import client.ui.view.panel.good.*;
import com.alibaba.fastjson.JSONObject;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;

public class GoodsPage extends FrameBase {
    private final List<PanelControl> panels = new ArrayList<>();
    private final Good goodInfo;
    public GoodsPage(Good goodInfo){
        this(goodInfo.getGoodName(),goodInfo);
    }

    public GoodsPage(String title, Good goodInfo) {
        super(title);
        this.setIconImage(Icons.shop.getImage());
        this.goodInfo = goodInfo;
        initial();
    }

    protected void initial(){
        initContentPanel();
        this.setBackground(MaterialColors.COSMO_LIGHT_GRAY);
        this.setSize(new Dimension(800,680));
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }

    /**
     * @Description 初始化商品显示页面
     * @author 孔明浩
     * @Date 2020/3/30
     */
    void initContentPanel(){
        //布局管理器
        GridBagLayout gridBagLayout = new GridBagLayout();
        GridBagConstraints gridBagConstraints;

        JPanel panel = new JPanel(gridBagLayout);
        panel.setBackground(MaterialColors.WHITE);

        //图片面板
        GoodImagePanel imagePanel = new GoodImagePanel(goodInfo);
        //添加到管理器
        panels.add(imagePanel);
        panel.add(imagePanel);
        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = GridBagConstraints.BOTH;
        gridBagConstraints.anchor = GridBagConstraints.NORTH;
        gridBagLayout.addLayoutComponent(imagePanel,gridBagConstraints);
        //描述面版
        GoodDescriptionPanel description = new GoodDescriptionPanel(goodInfo);
        panels.add(description);
        panel.add(description);
        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = GridBagConstraints.BOTH;
        gridBagConstraints.ipady = 100;
        gridBagConstraints.anchor = GridBagConstraints.NORTH;
        gridBagLayout.addLayoutComponent(description,gridBagConstraints);
        //购买面板
        BuyPanel buy = new BuyPanel(goodInfo, description);
        panels.add(buy);
        panel.add(buy);
        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = GridBagConstraints.NORTH;
        gridBagConstraints.fill = GridBagConstraints.VERTICAL;
        gridBagLayout.addLayoutComponent(buy,gridBagConstraints);
        //评论面板
        CommentPanel comment = new CommentPanel(goodInfo);
        panels.add(comment);
        panel.add(comment);
        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 0.75;
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = GridBagConstraints.BOTH;
        gridBagConstraints.anchor = GridBagConstraints.CENTER;
        gridBagLayout.addLayoutComponent(comment,gridBagConstraints);

        //编辑面板
        InputCommentPanel editPanel = new InputCommentPanel(comment,goodInfo);
        panels.add(editPanel);
        panel.add(editPanel);
        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 0.15;
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = GridBagConstraints.BOTH;
        gridBagConstraints.anchor = GridBagConstraints.CENTER;
        gridBagLayout.addLayoutComponent(editPanel,gridBagConstraints);

        this.setContentPane(panel);
        updatePanel("");
    }

    @Override
    public void updatePanel(String panelName) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                ResponseEntity responseEntity = Controller.GOOD_CONTROL.getGoodInfo(goodInfo.getGoodId());
                if (responseEntity.getCode() == 0){
                    final Good good = JSONObject.parseObject(responseEntity.getData(), Good.class);
                    for (PanelControl p:panels) {
                        SwingUtilities.invokeLater(new Runnable() {
                            @Override
                            public void run() {
                                p.updatePanel(good);
                            }
                        });
                    }
                }
            }
        }).start();
    }
}
